<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加轮播图' : '编辑轮播图'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="bannerInfo" :rules="rules" ref="bannerInfo">
        
        <el-form-item label="轮播图名称" :label-width="formLabelWidth" prop="catename">
          <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="onRemove"
            :on-change="onChange"
            :auto-upload="false"
            :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt>
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="dialogInfo.isAdd" type="primary" @click="addCate">添 加</el-button>
        <el-button v-else type="primary" @click="editMenu">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bannerAdd, getBannerEdit, getBannerInfo } from "../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      bannerInfo: {
        title: "", //分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1 //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        username: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        roleid: [{ required: true, message: "请选择角色", trigger: "change" }]
      },
      dialogVisible: false,
      dialogImageUrl: "",
      filelist: []
    };
  },
  props: ["dialogInfo"],
  computed: {
    ...mapGetters({
      bannerlist: "banners/getBannerList"
    })
  },
  methods: {
    //封装一个文件改变改变时触发的事件
    onChange(file, filelist) {
      console.log(file, "3333");
      console.log(filelist, "4444");
      this.bannerInfo.img = file.raw;
    },
    //封装一个图片预览方法
    onPreview(file) {
      console.log(file, "文件");
      this.dialogImageUrl = file.url;
      //弹框显示
      this.dialogVisible = true;
    },
    //图片移除
    onRemove() {},
    //获取商品分类列表行动
    ...mapActions({
      getBanner: "banners/getBannerListAction"
    }),
    //关闭弹窗方法
    cancel() {
      this.$emit("cancel", false);
      //重置
      this.reset();
    },
    //清空表单内容以及相关验证
    reset() {
      this.bannerInfo = {
       
        title: "", //分类名称
        img: "", //图片(文件，一般用于二级分类)
        status: 1 //状态1正常2禁用
      };
      //调用清空验证的方法
      this.$refs.bannerInfo.resetFields();
      //清空图片文件
      this.filelist = [];
    },
    //查询数据
    lookup(id) {
      getBannerInfo({ id }).then(res => {
        if (res.code == 200) {
          this.bannerInfo = res.list;
          this.bannerInfo.id = id;
          //图片数据回显
          this.filelist = [{url:this.$http + this.bannerInfo.img}]
        }else{
          this.$message.error(res.msg);
        }
      });
    },
    //确定事件
    addCate() {
      //验证
      this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          bannerAdd(this.bannerInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel(), 
              this.getBanner();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    //编辑事件
    editMenu() {
      this.$refs.bannerInfo.validate(valid => {
        if (valid) {
          getBannerEdit(this.bannerInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getBanner();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
</style>
